﻿@{
    ViewBag.Title = "计划任务";
}
@section header{

}
<div class="topPanel">
    <div class="toolbar">
        <div class="btn-group">
            <a class="btn btn-primary" onclick="$.reload()"><span class="glyphicon glyphicon-refresh"></span></a>
        </div>
        <div class="btn-group">
            <a id="NF-add" authorize="yes" class="btn btn-primary" onclick="btn_add()"><i class="fa fa-plus"></i>新建</a>
        </div>
        <div class="operate">
            <ul class="nav nav-pills">
                <li class="first">已选中<span>1</span>项</li>
                <li><a id="NF-edit" authorize="yes" onclick="btn_edit()"><i class="fa fa-pencil-square-o"></i>修改</a></li>
                <li><a id="NF-delete" authorize="yes" onclick="btn_delete()"><i class="fa fa-trash-o"></i>删除</a></li>
                <li><a id="NF-run" authorize="yes" onclick="btn_run()"><i class="fa fa-trash-o"></i>RUN</a></li>
            </ul>
            <a href="javascript:;" class="close"></a>
        </div>
        <script>$('.toolbar').authorizeButton()</script>
    </div>
    <div class="search">
        <table>
            <tr>
                <td>
                    <div class="input-group">
                        <input id="txt_keyword" type="text" class="form-control" placeholder="请输入要查询关键字" style="width: 200px;">
                        <span class="input-group-btn">
                            <button id="btn_search" type="button" class="btn  btn-primary"><i class="fa fa-search"></i></button>
                        </span>
                    </div>
                </td>
            </tr>
        </table>
    </div>
</div>
<div class="gridPanel">
    <table id="gridList"></table>
    <div id="gridPager"></div>
</div>

@section scripts{
    <script>
        var baseUrl = "/Sys/ScheduleTask/";
        $(function () {
            gridList();
        })
        function gridList() {
            var $gridList = $("#gridList");
            $gridList.dataGrid({
                url: baseUrl + "GetJGridPageDataJson",
                datatype: "json",
                height: $(window).height() - 128,
                colModel: [
                    { label: "主键", name: "ID", hidden: true, key: true },
                    { label: '任务名称', name: 'Name', align: 'left' },
                    { label: '运行周期', name: 'Seconds', align: 'left' },
                    { label: '任务类型', name: 'Type', align: 'left' },
                    { label: '启用', name: 'Enabled', align: 'left' },
                    {
                        label: "启用", name: "Enabled", width: 100, align: "center",
                        formatter: function (cellvalue, options, rowObject) {
                            var keyId = rowObject.ID;
                            return cellvalue == 0 ? "<i onclick=\"UpdateField('" + keyId + "','Enabled',0)\" class=\"fa fa-toggle-on\"></i>" :
                                "<i onclick=\"UpdateField('" + keyId + "','Enabled',1)\" class=\"fa fa-toggle-off\"></i>";
                        }
                    },
                    {
                        label: "停止任务", name: "StopOnError", width: 100, align: "center",
                        formatter: function (cellvalue, options, rowObject) {
                            var keyId = rowObject.ID;
                            return cellvalue == 0 ? "<i onclick=\"UpdateField('" + keyId + "','StopOnError',0)\" class=\"fa fa-toggle-on\"></i>" :
                                "<i onclick=\"UpdateField('" + keyId + "','StopOnError',1)\" class=\"fa fa-toggle-off\"></i>";
                        }
                    },
                    { label: '上次启动日期', name: 'LastStartUtc', align: 'left' },
                    { label: '上次完成日期', name: 'LastEndUtc', align: 'left' },
                    { label: '上次成功完成日期', name: 'LastSuccessUtc', align: 'left' }
                ],
                rowNum: 20,
                rowList: [20, 50, 100],
                pager: '#gridPager',
                sortname: 'OrderBy',
                sortorder: "asc",
                viewrecords: true,
                autowidth: true
            });
            $("#btn_search").click(function () {
                $gridList.jqGrid('setGridParam', {
                    postData: { keyword: $("#txt_keyword").val() },
                }).trigger('reloadGrid');
            });
        }
        function btn_add() {
            $.modalOpen({
                id: "Form",
                title: "新增",
                url: baseUrl + "Form",
                width: "700px",
                height: "440px",
                callBack: function (iframeId) {
                    top.frames[iframeId].submitForm();
                }
            });
        }
        function btn_edit() {
            var keyValue = $("#gridList").jqGridRowValue().ID;
            $.modalOpen({
                id: "Form",
                title: "修改",
                url: baseUrl + "Form?keyValue=" + keyValue,
                width: "700px",
                height: "440px",
                callBack: function (iframeId) {
                    top.frames[iframeId].submitForm();
                }
            });
        }
        function btn_delete() {
            $.deleteForm({
                url: baseUrl + "DeleteForm",
                param: { keyValue: $("#gridList").jqGridRowValue().ID },
                success: function () {
                    $.currentWindow().$("#gridList").resetSelection();
                    $.currentWindow().$("#gridList").trigger("reloadGrid");
                }
            })
        }
        function btn_run() {
            $.deleteForm({
                prompt: "注：您确定要执行该任务吗？",
                loading: "正在执行任务...",
                url: baseUrl + "RunNow",
                param: { keyValue: $("#gridList").jqGridRowValue().ID },
                success: function () {
                    $.currentWindow().$("#gridList").resetSelection();
                    $.currentWindow().$("#gridList").trigger("reloadGrid");
                }
            })
        }
        function Switch(keyId) {
            $.ajax({
                url: baseUrl + "Switch",
                data: { keyId: keyId },
                type: "POST",
                dataType: "json",
                async: false,
                success: function () {
                    $.currentWindow().$("#gridList").resetSelection();
                    $.currentWindow().$("#gridList").trigger("reloadGrid");
                }
            });
        }
        function UpdateField(keyId, fieldName, fieldValue) {
            $.ajax({
                url: baseUrl + "UpdateField?keyId=" + keyId,
                data: { keyId: keyId, fieldName: fieldName, fieldValue: fieldValue },
                type: "POST",
                dataType: "json",
                async: false,
                success: function () {
                    $.currentWindow().$("#gridList").resetSelection();
                    $.currentWindow().$("#gridList").trigger("reloadGrid");
                }
            });
        }
    </script>
}